/*
 * Copyright (c) 2017 22420103@QQ.COM 版权所有
 *
 */

package com.lehe.common.utils;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 
 * @author 王丹
 */
public class ExpireDate {

    private static String key = "lcjt12#";

    private static String simpleDateFormatPattern = "yyyy-MM-dd_HH:mm:ss";
    protected static SimpleDateFormat simpleDateFormat = new SimpleDateFormat(simpleDateFormatPattern);

    private static String decimalFormatPattern = "0000000000";
    private static DecimalFormat decimalFormat = new DecimalFormat(decimalFormatPattern);

    private final Date _date;
    private final int _validPeriod;
    private final Date _lastDate;
    private final boolean _isActive;

    public static void main(String[] args) throws Exception {
        //从2017-04-01_00:00:00开始使用，使用时间为0000000003(月)
        String date = "2015-01-31_00:00:00";
        //System.out.println(new ExpireDate(simpleDateFormat.parse(date), 48, true));

        //String copyright1 = EncryptUtil.desedeEncoder("2017-01-01_00:00:000000000012true", "lcjt12#");
        String copyright2 = EncryptUtil.desedeEncoder("2017-04-01_00:00:000000000009true", "lcjt12#");
        //String copyright3 = EncryptUtil.desedeEncoder("2017-01-01_00:00:000000000004true", "lcjt12#");
        //System.out.println(copyright1);
        System.out.println(copyright2);
        //System.out.println(ExpireDate.valueof(copyright1).isExpired());
        System.out.println(ExpireDate.valueof(copyright2).isExpired());
        //System.out.println(ExpireDate.valueof(copyright3).isExpired());
    }

    /* ----------------------------------------------------------------------------------------- */

    /**
     * 
     * @param date 开始使用日期
     * @param validPeriod 有效期
     * @param isActive 是否激活
     */
    protected ExpireDate(Date date, int validPeriod, boolean isActive) {

        _date = date;
        _validPeriod = validPeriod;
        _isActive = isActive;

        Calendar calendar = Calendar.getInstance();
        calendar.clear();

        calendar.setTime(_date);
        if (_isActive) {
            calendar.add(Calendar.MONTH, _validPeriod);
        } else {
            calendar.add(Calendar.MONTH, Integer.MAX_VALUE);
        }

        _lastDate = calendar.getTime();
    }

    /* ----------------------------------------------------------------------------------------- */

    public static ExpireDate valueof(String enExpireDate) {

        try {
            String text = EncryptUtil.desedeDecoder(enExpireDate, key);
            Date date = simpleDateFormat.parse(text.substring(0, simpleDateFormatPattern.length()));
            int validPeriod = Integer.valueOf(text.substring(simpleDateFormatPattern.length(), simpleDateFormatPattern.length() + decimalFormatPattern.length()));
            boolean isActive = Boolean.valueOf(text.substring(simpleDateFormatPattern.length() + decimalFormatPattern.length()));
            if (!isActive && validPeriod != 100) {
                throw new Exception("Wrong license.");
            }

            return new ExpireDate(date, validPeriod, isActive);
        } catch (Exception e) {
            return new ExpireDate(new Date(), Integer.MIN_VALUE, true);
        }
    }

    public boolean isExpired() {

        if (_date.after(new Date())) {
            return true;
        }

        return _lastDate.before(new Date());
    }

    @Override
    public String toString() {

        try {
            String expireDate = simpleDateFormat.format(_date);
            return EncryptUtil.desedeEncoder(expireDate + decimalFormat.format(_validPeriod) + _isActive, key);
        } catch (Exception e) {
            return null;
        }
    }

   private void mian() throws Exception {

       String expireDate = simpleDateFormat.format(new Date());
       String de = EncryptUtil.desedeEncoder(expireDate, "Clochase1!");
       System.out.println(de);
       String dec = EncryptUtil.desedeDecoder(de, key);
       System.out.println(expireDate + "======" + de + ":********" + dec);

   }


}
